package com.holyn.guofang.viewcommon.radiobutton;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatRadioButton;
import android.util.AttributeSet;
import android.view.Gravity;

/**
 * Created by Holyn on 2017/7/21.
 */
public class DrawableCenterRadioButton extends AppCompatRadioButton {
    public DrawableCenterRadioButton(Context context) {
        super(context);
    }

    public DrawableCenterRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawableCenterRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        Drawable[] drawables = getCompoundDrawables();
//        if (drawables != null) {
//            Drawable drawableLeft = drawables[0];
//            if (drawableLeft != null) {
//                float textWidth = getPaint().measureText(getText().toString());
//                int drawablePadding = getCompoundDrawablePadding();
//                int drawableWidth = 0;
//                drawableWidth = drawableLeft.getIntrinsicWidth();
//                float bodyWidth = textWidth + drawableWidth + drawablePadding;
//                canvas.translate((getWidth() - bodyWidth) / 2, 0);
//            }
//        }
        super.onDraw(canvas);

        Drawable[] drawables = getCompoundDrawables();
        if (drawables != null){
            Drawable drawableLeft = drawables[0];
            int drawableWidth = drawableLeft.getIntrinsicWidth();
            float textWidth = getPaint().measureText(getText().toString());
            int drawablePadding = getCompoundDrawablePadding();
            int gravity = getGravity();
            int left = 0;
            if (gravity == Gravity.CENTER) {
                left = ((int) (getWidth() / 2 - textWidth/2)-drawablePadding-drawableWidth);
            }
            drawableLeft.setBounds(left,0,left+drawableLeft.getIntrinsicWidth(),drawableLeft.getIntrinsicHeight());
        }

    }

}
